﻿using System;
using System.Windows.Forms;

namespace DosTexPrj.UI.WinForm
{
    public partial class MainForm : Form
    {
        private bool _isLogin;

        public MainForm()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            ShowLoginForm();
        }

        private void menuCustomerOrderList_Click(object sender, EventArgs e)
        {
            if (this._isLogin)
            {
                OpenForm(new CustomerOrderForm());
            }
            else
            {
                ShowLoginForm();
            }
        }

        private void menuYarnOrderList_Click(object sender, EventArgs e)
        {
            if (this._isLogin)
            {
                OpenForm(new YarnOrderForm());
            }
            else
            {
                ShowLoginForm();
            }
        }

        private void menuKnittingOrder_Click(object sender, EventArgs e)
        {
            if (this._isLogin)
            {
                OpenForm(new KnittingOrderForm());
            }
            else
            {
                ShowLoginForm();
            }
        }

        private void menuDyeOrder_Click(object sender, EventArgs e)
        {
            if (this._isLogin)
            {
                OpenForm(new DyeOrderForm());
            }
            else
            {
                ShowLoginForm();
            }
        }

        private void menuDelivery_Click(object sender, EventArgs e)
        {
            if (this._isLogin)
            {
                OpenForm(new DeliveryForm());
            }
            else
            {
                ShowLoginForm();
            }
        }

        private void menuKnittingDemo_Click(object sender, EventArgs e)
        {
            if (this._isLogin)
            {
                OpenForm(new KnittingDemoForm());
            }
            else
            {
                ShowLoginForm();
            }
        }

        private void menuLogout_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure want to log out?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this._isLogin = false;
                CloseAllForms();
                ShowLoginForm();
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            ShowLoginForm();
        }

        #region Functions

        public void OpenForm(Form form)
        {
            Boolean flag = false;
            foreach (Form f in Application.OpenForms)
            {
                if (form.Name == f.Name)
                {
                    form.Activate();
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                form.MdiParent = this;
                form.StartPosition = FormStartPosition.CenterScreen;
                form.Show();
            }
        }

        private void ShowLoginForm()
        {
            LoginForm formLogin = new LoginForm();
            formLogin.FormClosed += new FormClosedEventHandler(formLogin_FormClosed);
            formLogin.StartPosition = FormStartPosition.CenterParent;
            formLogin.ShowDialog();
        }

        private void formLogin_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!(sender as LoginForm).IsLogin)
            {
                this._isLogin = false;
                menuLogout.Enabled = false;
                btnLogin.Enabled = true;
                return;
            }
            this._isLogin = (sender as LoginForm).IsLogin;
            menuLogout.Enabled = true;
            btnLogin.Enabled = false;
        }

        private void CloseAllForms()
        {
            foreach (Form form in OpenForms)
            {
                if (form.Name != "MainForm")
                {
                    CloseForm(form);
                }
            }
        }

        delegate void CloseMethod(Form form);
        private void CloseForm(Form form)
        {
            if (!form.IsDisposed)
            {
                if (form.InvokeRequired)
                {
                    CloseMethod method = new CloseMethod(CloseForm);
                    form.Invoke(method, new object[] { form });
                }
                else
                {
                    form.Close();
                }
            }
        }

        private Form[] OpenForms
        {
            get
            {
                Form[] forms = null;
                int count = Application.OpenForms.Count;
                forms = new Form[count];
                if (count > 0)
                {
                    int index = 0;
                    foreach (Form form in Application.OpenForms)
                    {
                        forms[index++] = form;
                    }
                }
                return forms;
            }
        }

        #endregion Functions
    }
}